public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }
    public TreeNode root = null;
    //模拟实现搜索二叉树的搜索功能
    public TreeNode find(int val){
        TreeNode cur = root;
        while ( cur != null){
            //如果val值比cur的值大，说明要在搜索树右边搜索
            if(cur.val < val){
                cur = cur.right;
            }else if(cur.val == val){
                return cur;
            }else {
                //如果val值比cur的值小，说明要在搜索树右边搜索
                cur = cur.left;
            }
        }
        //什么都没找到
        return null;
    }
    //模拟实现二叉树的插入功能
    public boolean insert(int val){
        if(root == null){
            root = new TreeNode(val);
            return true;
        }
        TreeNode cur = root;
        TreeNode prevcur = null;
        TreeNode node = new TreeNode(val);
        while (cur != null){
            //大于cur的值，往右走,根节点左边的值都是小于根节点的，右边的值都是大于根节点的.
            if(cur.val < val){
                prevcur = cur;
                cur = cur.right;
            }else if(cur.val == val){
                //二叉搜索树当中出现重复值是无意义的，此处可视为去重
                return false;
            }else{
                prevcur = cur;
                cur = cur.left;
            }
        }
        if(prevcur.val < val){
            prevcur.right = node;
        }
        if (prevcur.val > val){
            prevcur.left = node;
        }
        return true;
    }
    //中序遍历打印二叉搜索树
    public void inorder(TreeNode root){
        if(root == null){
            return;
        }
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.right);
    }
    public void remove(int val){
        TreeNode cur = root;
        TreeNode prevcur = null;
        while (cur != null){
            if(cur.val == val){
                removeNode(prevcur,cur);
                return;
            }else if(cur.val < val){
                prevcur = cur;
                cur = cur.right;
            }else {
                prevcur = cur;
                cur = cur.left;
            }
        }
    }

    private void removeNode(TreeNode prevcur, TreeNode cur) {
        //找到节点位置的左树为空
        if(cur.left == null){
            if(cur == root){
                //左树为空并且cur为根节点的情况下
                root = cur.right;
            }else if(prevcur.right == cur){
                //cur在父节点右边,cur左树为空并且不为根节点的情况下
                prevcur.right = cur.right;
            }else{
                //cur在父节点左边,cur左树为空并且不为根节点的情况下
                prevcur.left = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = cur.left;
            }else if(prevcur.right == cur){
                prevcur.right = cur.left;
            }else {
                prevcur.left = cur.left;
            }
        }else {
            TreeNode target = cur.right;
            TreeNode prevtarget = cur;
            while (target.left != null){
                prevtarget = target;
                target = target.left;
            }
            cur.val = target.val;
            if(prevtarget.left == target){
                prevtarget.left = target.right;
            }
            if(prevtarget.right == target){
                prevtarget.right = target.right;
            }
        }
    }
}
